package com.campus.controller;

import com.campus.common.CampusWall;
import com.campus.common.Result;
import com.campus.entity.Posts;
import com.campus.entity.Users;
import com.campus.service.PostService;
import com.campus.utils.SnowFlakeUtil;
import com.campus.utils.TokenUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class PostController {
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private PostService postService;

    /**
     * 上传帖子
     *
     * @param post
     * @param token
     * @return
     */
    @PostMapping("/post/create")
    public Result createPost(@RequestBody Posts post, @RequestHeader(CampusWall.CAMPUS_TOKEN) String token) {
        Result result = postService.createPost(post, token);

        return result;
    }

    /**
     * 查询帖子
     *
     * @param page
     * @param size
     * @param category
     * @return
     */
    @GetMapping("/post/list")
    public Result getPostList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(defaultValue = "0") Integer category
    ) {
        // 如果category为0，表示查询所有帖子
        Result result = postService.getPostsList(page, size, category);
        return result;
    }

    @GetMapping("post/{postId}")
    public Result getPostById(@PathVariable("postId") String postId) {
        Result result = postService.getPostDetailById(postId);
        return result;
    }

    @GetMapping("/post/user/{userId}")
    public Result getUserPosts(@PathVariable("userId") String userId, 
                              @RequestParam(defaultValue = "1") Integer page, 
                              @RequestParam(defaultValue = "10") Integer size, 
                              HttpServletRequest request) {
        Result result = postService.getUserPosts(userId, size, page, request);
        return result;
    }
    
    /**
     * 删除帖子
     *
     * @param postId 帖子ID
     * @param request HTTP请求
     * @return 删除结果
     */
    @DeleteMapping("/post/{postId}")
    public Result deletePost(@PathVariable("postId") String postId, HttpServletRequest request) {
        // 获取当前用户
        String token = tokenUtils.getToken(request.getHeader(CampusWall.CAMPUS_TOKEN));
        Users currentUser = tokenUtils.getCurrentUser(token);
        
        if (currentUser == null) {
            return Result.err(Result.CODE_401, "用户未登录");
        }
        
        // 调用服务层删除帖子
        Result result = postService.deletePost(postId, currentUser.getUserId());
        return result;
    }

    /**
     * 更新帖子
     *
     * @param postId 帖子ID
     * @param post   更新内容（标题/内容/分类/图片）
     * @param request HTTP请求
     * @return 更新结果
     */
    @PutMapping("/post/{postId}")
    public Result updatePost(@PathVariable("postId") String postId,
                             @RequestBody Posts post,
                             HttpServletRequest request) {
        String token = tokenUtils.getToken(request.getHeader(CampusWall.CAMPUS_TOKEN));
        Users currentUser = tokenUtils.getCurrentUser(token);

        if (currentUser == null) {
            return Result.err(Result.CODE_401, "用户未登录");
        }

        return postService.updatePost(postId, post, currentUser.getUserId());
    }
}
